﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;

namespace DrawLine
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        Stopwatch _stopwatch = new Stopwatch();

        int _second;

        int _counter;

        public MainWindow()
        {
            InitializeComponent();

            CompositionTarget.Rendering += CompositionTarget_Rendering;

            _stopwatch.Start();
        }

        private void ResetFPS()
        {
            _second = 0;
            _counter = 0;
            _stopwatch.Restart();
        }

        private void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            _drawer.InvalidateVisual();
            _counter++;

            int second = (int)(_stopwatch.ElapsedMilliseconds / 1000);
            if (_second != second)
            {
                _fps.Text = Math.Round((double)_counter / second, 2).ToString();
                _second = second;
            }
            // 5分で1回リセットします。
            if( second > 300 )
            {
                ResetFPS();
            }
        }

        private void ThicknessSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            ResetFPS();
        }

        private void LineCountSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            ResetFPS();
        }
    }
}
